package com.ithaipeng.redanttakeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithaipeng.redanttakeaway.dto.DishDto;
import com.ithaipeng.redanttakeaway.exception.CustomException;
import com.ithaipeng.redanttakeaway.mapper.DishMapper;
import com.ithaipeng.redanttakeaway.pojo.Dish;
import com.ithaipeng.redanttakeaway.pojo.DishFlavor;
import com.ithaipeng.redanttakeaway.pojo.Setmeal;
import com.ithaipeng.redanttakeaway.pojo.SetmealDish;
import com.ithaipeng.redanttakeaway.service.IDishFlavorService;
import com.ithaipeng.redanttakeaway.service.IDishService;
import com.ithaipeng.redanttakeaway.service.ISetmealDishService;
import com.ithaipeng.redanttakeaway.service.ISetmealService;
import com.ithaipeng.redanttakeaway.tools.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {
    @Autowired
    private IDishFlavorService dishFlavorService;
    @Autowired
    private ISetmealDishService setmealDishService;
    @Autowired
    private ISetmealService setmealService;
    @Override
    public void insertWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
        List<DishFlavor> flavors = dishFlavorList.stream().peek((item) -> {
            Long id = dishDto.getId();
            item.setDishId(id);
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto=new DishDto();
        //复制
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        LambdaUpdateWrapper<SetmealDish> setmealDishLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        //删除后再保存
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> dishFlavors = flavors.stream().peek((item) ->
                item.setDishId(dishDto.getId())).collect(Collectors.toList());
        dishFlavorService.saveBatch(dishFlavors);
        //再将setmeal 的信息修改
        setmealDishLambdaUpdateWrapper.eq(SetmealDish::getDishId,dishDto.getId());
        setmealDishLambdaUpdateWrapper.set(SetmealDish::getName,dishDto.getName());
        setmealDishLambdaUpdateWrapper.set(SetmealDish::getPrice,dishDto.getPrice());
        setmealDishLambdaUpdateWrapper.set(SetmealDish::getUpdateTime,dishDto.getUpdateTime());
        setmealDishLambdaUpdateWrapper.set(SetmealDish::getUpdateUser,dishDto.getUpdateUser());
        setmealDishService.update(setmealDishLambdaUpdateWrapper);
    }

    @Override
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        int count1 = setmealDishService.count(setmealDishLambdaQueryWrapper);
        int count = this.count(dishLambdaQueryWrapper);
        if(count>0||count1>0){
            throw  new CustomException("删除的菜品有正在销售的或者关联了其他套餐，不能删除!");
        }else {
            //当什么也没发生时，先删dish表，再将对应套餐进行删除
            this.removeByIds(ids);
            List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);
            List<Long> setmealIds = setmealDishes.stream().map(SetmealDish::getSetmealId).collect(Collectors.toList());
            LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
            setmealLambdaQueryWrapper.in(!setmealIds.isEmpty(),Setmeal::getId,setmealIds);
            setmealLambdaQueryWrapper.eq(Setmeal::getStatus,0);
            setmealService.remove(setmealLambdaQueryWrapper);
            setmealDishService.remove(setmealDishLambdaQueryWrapper);
        }
    }

    @Override
    public void upDateStatusByIds(Integer status, List<Long> ids) {
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        int count = setmealDishService.count(setmealDishLambdaQueryWrapper);
        if(count>0){
            throw new CustomException("该菜品关联了套餐不能进行操作!");
        }
        LambdaUpdateWrapper<Dish> dishLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        dishLambdaUpdateWrapper.set(Dish::getStatus,status);
        dishLambdaUpdateWrapper.set(Dish::getUpdateTime, LocalDateTime.now());
        dishLambdaUpdateWrapper.set(Dish::getUpdateUser, BaseContext.getCurrentId());
        dishLambdaUpdateWrapper.in(Dish::getId,ids);
        this.update(dishLambdaUpdateWrapper);
    }
}
